#!/bin/sh

#VAD: enabling (driver upgrade mode)

PREF_PATH=/System/Library/PreferencePanes/Ink.prefPane
#check for existing prefpane (Jaguar and higher)
if [ -e "$PREF_PATH"/Contents/MacOS ]; then
        cd /System/Library/PreferencePanes/Ink.prefPane/Contents
	mv .Info.old Info.plist
	cp Info.plist .Info.old
	cd /System/Library/PreferencePanes/ACTIVboard.prefPane/Contents
# will not replace, when source not found
	/System/Library/PreferencePanes/ACTIVboard.prefPane/Contents/PlugIns/Ink_corrector
	rm ~/Library/Caches/com.apple.preferencepanes.cache
fi

rm /System/Library/PreferencePanes/ACTIVboard.prefPane/Contents/PlugIns/Ink_corrector

rm ~/Library/Preferences/com.promethean.virtualvoteserver.plist

chmod +x /System/Library/Extensions/IOACTIV.kext/Contents/PlugIns/ACTIVlibrary.bundle/Contents/MacOS/ACTIVlibrary 
chmod +x "/System/Library/Extensions/IOACTIV.kext/Contents/PlugIns/ACTIVirtual Vote Server.app/Contents/PlugIns/DNSPlugin.bundle/Contents/MacOS/DNSPlugin"
chmod +x "/System/Library/PreferencePanes/ACTIVboard.prefPane/Contents/PlugIns/ACTIVport.bundle/Contents/MacOS/ACTIVport"

#check for Puma
if [ ! -e "$PREF_PATH"/Contents/MacOS ]; then
    rm -R -f "$PREF_PATH"
fi

#VAD: creating calibration preferences folder
umask 0
mkdir -p /Users/Shared/Library/Preferences


# VAD: loading kernel extension
# kextunload -v /System/Library/Extensions/IOACTIV.kext
# kextload -v /System/Library/Extensions/IOACTIV.kext
/usr/bin/touch /System/Library/Extensions
/usr/bin/touch /System/Library/PreferencePanes
/usr/bin/touch /System/Library/StartupItems

rm /tmp/activb*/
rm /System/Library/Extensions.mkext
rm /System/Library/Extensions.kextcache
rm /Library/Preferences/com.prometheanworld.activsupport.plist

mv -f /Applications/Promethean/Activstudio3/libactivboardex.dylib /usr/lib
chmod +x /usr/lib/libactivboardex.dylib

mkdir -p /var/Promethean/fwhistory
chmod 0777 /var/Promethean
chmod 0777 /var/Promethean/fwhistory

if [ ! -e /usr/share/promethean ] ; then
	mkdir -p -m 0755 /usr/share/promethean
fi	
cp -R -f /Applications/Promethean/Activstudio3/languages /usr/share/promethean
# Check result...
if [ $? -ne 0 ] ; then
	echo "ERROR: failed to copy $dest/hwr /Builds/Inspire/HWR/Files/Promethean/Activsoftware Inspire"
else
	rm -rdf /Applications/Promethean/Activstudio3/languages
fi

LWPL="/Library/Preferences/loginwindow.plist"
TLWPL="/tmp/loginwindow.plist"

echo > $TLWPL

if [ ! -e $LWPL ]; then

echo Creating system login startup file...
cat << EOF >> $TLWPL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>AutoLaunchedApplicationDictionary</key>
	<array>
		<dict>
			<key>Hide</key>
			<false/>
			<key>Path</key>
			<string>/Applications/Promethean/activmgr.app</string>
		</dict>
	</array>
</dict>
</plist>
EOF

cp $TLWPL $LWPL
plutil -convert binary1 $LWPL
exit 0
fi

if [ -w $LWPL ]; then
	#convert file to xml for text processing
	plutil -convert xml1 $LWPL
	found=0
	if [ `grep activmgr $LWPL` ]; then
		echo Activmanager is already in the login list
		found=1
	fi
	
	echo Adding Activmanager system to login startup file...
	# loginwindow.plist exists but does not have activmanager
	cat $LWPL | while read line; do
		echo $line
		if [ `echo $line|grep "<array/>"` ] && [ $found -eq 1 ] ; then
				echo Found line array/

cat << EOF >> $TLWPL
	<array>
		<dict>
		<key>Hide</key>
			<false/>
			<key>Path</key>
			<string>/Applications/Promethean/activmgr.app</string>
		</dict>
	</array>
</dict>	
</plist>	
EOF

exit 0

		elif [ `echo $line|grep "<array/>"` ] && [ $found -eq 0 ] ; then
				echo Found line array/

cat << EOF >> $TLWPL
	<array>
		<dict>
		<key>Hide</key>
			<false/>
			<key>Path</key>
			<string>/Applications/Promethean/activmgr.app</string>
		</dict>
	</array>
</dict>	
</plist>	
EOF

exit 0

		elif [ `echo $line|grep "<array>"` ] && [ $found -eq 0 ] ; then
				echo Found line array

cat << EOF >> $TLWPL
	<array>
		<dict>
		<key>Hide</key>
			<false/>
			<key>Path</key>
			<string>/Applications/Promethean/activmgr.app</string>
		</dict>
EOF

		else
			echo $line >> $TLWPL
		fi		
	done
	
	cp $TLWPL $LWPL
	plutil -convert binary1 $LWPL
else
	echo Cannot open $LWPL for writing.
fi


exit 0